use axum::{extract::Request, middleware::Next, response::{IntoResponse, Response}};
use crate::utils::{result::err, jwt::is_token_expired};


pub async fn auth_fn_mid(req: Request, next: Next) -> Result<Response, impl IntoResponse> {
    let ctx = req.headers().get("authorization");
    match ctx {
        Some(bearer) => {
            // 切割出jwt
            let token = &bearer.to_str().unwrap()[7..];
            println!("authorization: {:?}", token);
            // 如果设定的时间小于当前时间
            if is_token_expired(token) {
                return Err(err(400, "登录已过期"));
            }
            Ok(next.run(req).await)
        },
        None => Err(err(401, "用户未登录"))
    }
}